package com.example.weather_app.city_manager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.weather_app.MainActivity;
import com.example.weather_app.R;
import com.example.weather_app.base.BaseActivity;
import com.example.weather_app.bean.WeatherBean;
import com.google.gson.Gson;

public class SearchCityActivity extends BaseActivity implements View.OnClickListener {

    private ImageView submitIv;
    private EditText searchEt;
    private GridView searchGv;

    String[] hotCitys = {"北京", "上海", "广州", "深圳", "珠海", "佛山", "南京", "苏州", "厦门", "长沙", "成都", "福州",
            "杭州", "武汉", "青岛", "西安", "太原", "沈阳", "重庆", "天津", "南宁"};

    String city;

    //网络请求天气api路径
    private String s1 = "https://apis.juhe.cn/simpleWeather/query?city=";
    private String s2 = "&key=f53ed2315c2d7f31c0a99c76956ce1be";

    private ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_city);

        initPager();
        searchGv.setAdapter(arrayAdapter);
        submitIv.setOnClickListener(this);
        //对热门城市进行监听事件
        searchGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int postion, long id) {
                city = hotCitys[postion];
                String url = s1 + city + s2;
                loadPath(url);
            }
        });
    }

    private void initPager() {
        submitIv = findViewById(R.id.search_iv_submit);
        searchEt = findViewById(R.id.search_et);
        searchGv = findViewById(R.id.search_gv);
        arrayAdapter = new ArrayAdapter(this, R.layout.item_hotcity, hotCitys);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_iv_submit:
                city = searchEt.getText().toString();
                if (!TextUtils.isEmpty(city)) {
                    //解析判断返回值
                    String url = s1 + city + s2;
                    loadPath(url);
                } else {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    //解析返回值
    @Override
    public void onSuccess(String result) {
        //解析result存入数据实体
        WeatherBean weatherBean = new Gson().fromJson(result, WeatherBean.class);
        if (weatherBean.getError_code()!=0) {
            //没有城市信息
            Toast.makeText(this, "此城市暂未收录", Toast.LENGTH_SHORT).show();
        }else {
            //返回MainActivity并重新加载
            Intent it = new Intent(this, MainActivity.class);
            it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            it.putExtra("city", city);
            startActivity(it);
        }
    }
}